﻿Imports Microsoft.VisualBasic
Imports Aras.IOM
Imports MSXML2
Imports System.Xml
Imports System.IO



Public Class ConnecteurAras

    Dim conn As HttpServerConnection
    Dim inn As Innovator

    Public Sub New(ByVal url As String, _
                   ByVal db As String, _
                   ByVal InnovUser As String, _
                   ByVal pass As String)



        'pass = Innovator.ScalcMD5(pass)
        conn = IomFactory.CreateHttpServerConnection(url, db, InnovUser, pass)
        Dim login_result As Item = conn.Login


        If login_result.isError Then
            Throw New Exception("login failed")
        End If

        inn = IomFactory.CreateInnovator(conn)

    End Sub


    Public Function getItemProperties(ByVal tablename As String) As Item

        Dim Answer As Item
        Dim SQL As String
        SQL = "SELECT * FROM INFORMATION_SCHEMA.COLUMNS  where TABLE_NAME= '" & tablename & "'"
        Answer = Me.inn.applySQL(SQL)

        Return Answer

    End Function

    Public Function addfile(ByVal FileName As String) As String
        Dim result As String = ""

        Dim qryitem As Item = Me.inn.newItem("DossierDocument", "add")
        Dim subitem As Item = Me.inn.newItem("Document", "add")
        subitem.setProperty("FileName", FileName)

        Dim results As Item = qryitem.apply()

        Return result

    End Function

    Public Function UnlockDoc(ByVal ItemId As String) As String

        Dim qryitem As Item = Me.inn.newItem("Document", "unlock")
        qryitem.setAttribute("id", ItemId)
        Dim results As Item = qryitem.apply()
        Dim resultat As String
        If results.isError Then
            resultat = "error"
        Else
            resultat = results.getItemsByXPath("//Result/Item").getID
        End If
        Return resultat
    End Function

    Public Function LockDoc(ByVal ItemId As String) As String

        Dim qryitem As Item = Me.inn.newItem("Document", "lock")
        qryitem.setAttribute("id", ItemId)
        Dim results As Item = qryitem.apply()
        Dim resultat As String
        If results.isError Then
            resultat = "error"
        Else
            resultat = results.getItemsByXPath("//Result/Item").getID
        End If
        Return resultat
    End Function

    Public Function UploadNewDoc(ByVal ItemId As String, ByVal filepath As String, ByVal filename As String) As String
        'Document should be locked
        'upload
        Dim qryitem As Item = Me.inn.newItem("Document", "update")
        qryitem.setAttribute("id", ItemId)
        Dim rel As Item = Me.inn.newItem("Document File", "add")
        Dim DocFile As Item = Me.inn.newItem("File", "add")
        DocFile.setProperty("filename", filename)
        DocFile.attachPhysicalFile(filepath)
        qryitem.addRelationship(rel)
        rel.setRelatedItem(DocFile)

        Dim results As Item = qryitem.apply()
        ' Dim FileID As String = results.getItemsByXPath("//Result/Item").getRelationships.getItemByIndex(0).getRelatedItemID


        Dim resultat As String
        If results.isError Then
            resultat = "error"
        Else
            resultat = "save & upload are done"
        End If

        Me.LockDoc(ItemId)

        Return results.getItemsByXPath("//Result/Item").getID

    End Function

    Public Function UploadUpdatedDoc(ByVal ItemId As String, ByVal filepath As String, ByVal filename As String) As String

        'Document should be locked
        'upload
        Dim results1 As Item
        Dim qryitem As Item = Me.inn.newItem("Document", "get")
        qryitem.setAttribute("id", ItemId)
        Dim rel As Item = Me.inn.newItem("Document File", "get")
        Dim DocFile As Item = Me.inn.newItem("File", "get")
        qryitem.addRelationship(rel)
        rel.setRelatedItem(DocFile)
        results1 = qryitem.apply()
        Dim FileID As String = results1.getItemsByXPath("//Result/Item").getRelationships.getItemByIndex(0).getRelatedItemID
        Dim DocFileID As String = results1.getItemsByXPath("//Result/Item").getRelationships.getItemByIndex(0).getID


        'results1 = qryitem.apply()

        qryitem = Me.inn.newItem("Document", "update")
        qryitem.setAttribute("id", ItemId)
        Dim relOld = Me.inn.newItem("Document File", "delete")
        relOld.setAttribute("id", DocFileID)
        Dim DocFileOld = Me.inn.newItem("File", "delete")
        DocFileOld.setAttribute("id", FileID)
        Dim relNew = Me.inn.newItem("Document File", "add")
        Dim DocFileNew = Me.inn.newItem("File", "add")
        DocFileNew.setProperty("filename", filename)
        DocFileNew.attachPhysicalFile(filepath)
        qryitem.addRelationship(relOld)
        qryitem.addRelationship(relNew)
        relOld.setRelatedItem(DocFileOld)
        relNew.setRelatedItem(DocFileNew)
        results1 = qryitem.apply()




        Return results1.getItemsByXPath("//Result/Item").getID




    End Function


    Public Function IsDocLocked(ByVal ItemNumber As String) As Integer

        Dim resultat As Integer = 2

        Dim qryitem As Item = Me.inn.newItem("Document", "get")
        qryitem.setAttribute("id", ItemNumber)
        Dim results As Item = qryitem.apply()

       resultat = results.getItemsByXPath("//Result/Item").getLockStatus
        Return resultat

    End Function


    Public Function DocHasFileTest(ByVal ItemId As String) As Boolean
        Dim resultat As Boolean = False

        Dim qryitem As Item = Me.inn.newItem("Document", "get")
        qryitem.setAttribute("id", ItemId)
        Dim rel As Item = Me.inn.newItem("Document File", "get")
        Dim DocFile As Item = Me.inn.newItem("File", "get")
        qryitem.addRelationship(rel)
        rel.setRelatedItem(DocFile)
        Dim results As Item = qryitem.apply()

        If results.getItemsByXPath("//Result/Item").getRelationships.nodeList.Count > 0 Then
            resultat = True
        End If

        Return resultat
    End Function

    Protected Overrides Sub Finalize()
        Try

            conn.Logout()
        Catch ex As Exception
            GoTo fin
        End Try
fin:
        MyBase.Finalize()
    End Sub
End Class
